import { BillStateErrorFilter } from './filter/billstate.filter';
import {
  MidwayValidationErrorFilter,
  MissTargetErrorFilter,
} from './filter/missTarget.filter';
import { Configuration, App, Config } from '@midwayjs/core';
import * as koa from '@midwayjs/koa';
import * as validate from '@midwayjs/validate';
import * as info from '@midwayjs/info';
import * as cors from '@midwayjs/cross-domain';
import * as swagger from '@midwayjs/swagger';
import * as typeorm from '@midwayjs/typeorm';
import { join } from 'path';
// import { DefaultErrorFilter } from './filter/default.filter';
// import { NotFoundFilter } from './filter/notfound.filter';
import { ReportMiddleware } from './middleware/report.middleware';
import {
  DecMiddleware,
  InterceptorMiddleware,
  LoginMiddleware,
} from './middleware/train.middleware';
@Configuration({
  imports: [
    koa,
    validate,
    cors,
    swagger,
    typeorm,
    {
      component: info,
      enabledEnvironment: ['local'],
    },
  ],
  importConfigs: [join(__dirname, './config')],
})
export class ContainerLifeCycle {
  @App()
  app: koa.Application;
  @Config('middleware.enableInterceptor')
  enableInterceptor: boolean;

  async onReady() {
    // add middleware
    this.app.getMiddleware().push(LoginMiddleware);
    this.app.getMiddleware().push(DecMiddleware);
    if (this.enableInterceptor) {
      this.app.getMiddleware().push(InterceptorMiddleware);
    }
    this.app.getMiddleware().push(ReportMiddleware);
    // this.app.useMiddleware([
    //   LoginMiddleware,
    //   DecMiddleware,
    //   // InterceptorMiddleware,
    //   ReportMiddleware,
    // ]);
    // add filter
    // this.app.useFilter([NotFoundFilter, DefaultErrorFilter]);
    this.app.useFilter([
      MissTargetErrorFilter,
      BillStateErrorFilter,
      MidwayValidationErrorFilter,
    ]);
  }
}
